<?php
/**
 * Description of UserMapper
 *
 /*
 * @author     Thibaut Cromphaut and Dimitry Dierickx
 * @copyright  Copyright (c) 2011 Safti-Kado
 */
class Application_Model_TipMapper extends Ahs_Model_Mapper
{

    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_Tips';
    }

    /**
     * @param integer $id
     * @return Application_Model_Tip
     */
    public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array('tip_id = ?' => $id));

        //return $this->_totip($row);
        return $row;
    }

    /**
     * @return array
     */
    public function fetchAll()
    {        
        $rows = $this->getDbTable()->fetchAll();
        
        return $this->_totips($rows);
    }

    /**
     * @param Application_Model_Tip $tip
     */
    public function save(Application_Model_Tip $tip)
    {
        $data = array(
            'tip_title'       => $tip->Title,
            'tip_description' => $tip->Description,
            'tip_content'     => $tip->Content,
            'tip_image'       => $tip->Image,
        );

        if (null == $tip->Id) {
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('tip_id = ?' => $tip->Id));
        }
    }
    
    /**
     * @param Zend_Db_Table_Row_Abstract $row
     * @return Application_Model_Tip 
     */
    private function _totip(Zend_Db_Table_Row_Abstract $row = null)
    {
        $tip = new Application_Model_Tip();

        
        if ($row) {
            $tip->Id           = $row['tip_id'];
            $tip->Title        = $row['tip_title'];
            $tip->Description  = $row['tip_description'];
            $tip->Content      = $row['tip_content'];
            $tip->Image        = $row['tip_image'];
        }
        
        return $tip;
    }

    /**
     * @param Zend_Db_Table_Rowset_Abstract $rows
     * @return array
     */
    private function _totips(Zend_Db_Table_Rowset_Abstract $rows = null)
    {
        $tips = array();

        if ($rows) {
            foreach ($rows as $row) {
                $tips[] = $this->_totip($row);
            }
        }
        
        return $tips;
    }

}
